function FreezeTower (x, y, monsterManger) {
  this.x = x;
  this.y = y;
  this.monsterManger = monsterManger;
  this.monsters = monsterManger.getMonsters();

  this.range = 200;
  this.fireTime = 1000;

  this.nextFire = 0;

  this.tower = null;
  this.init();
}

FreezeTower.prototype.init = function() {
  this.tower = game.add.sprite(this.x + 50, this.y + 50, 'imageList', 'attackTower.png');
  this.tower.anchor.set(0.5);

  this.bullets = game.add.group();
  this.bullets.enableBody = true;
  this.bullets.physicsBodyType = Phaser.Physics.ARCADE;
  this.bullets.createMultiple(100, 'imageList', 'bullet.png');
}

FreezeTower.prototype.update = function() {
  this.monsters = this.monsterManger.getMonsters();
  for(var i in this.monsters) {
    if (this.monsters[i].alive) {
      game.physics.arcade.overlap(this.bullets, this.monsters[i].sprite, this.hitMonster, null, this);
      var distance = this.monsters[i] ? game.physics.arcade.distanceBetween(this.tower, this.monsters[i].sprite) : 10000;
      if (distance <= this.range) {
        this.tower.rotation = game.physics.arcade.angleBetween(this.tower, this.monsters[i].sprite);
        this.fire(this.monsters[i].sprite);
        return ;
      }
    }
    
  }
  

}

FreezeTower.prototype.fire = function(monster) {
  if (game.time.now > this.nextFire && this.bullets.countDead() > 0)
    {
        this.nextFire = game.time.now + this.fireTime;

        var bullet = this.bullets.getFirstExists(false);

        bullet.reset(this.tower.x, this.tower.y);

        bullet.rotation = game.physics.arcade.moveToObject(bullet, monster, 700);
    }
};

FreezeTower.prototype.hitMonster = function(monster,bullet) {
  console.log(monster)
  bullet.kill();

  if (this.monsters[monster.id]) {
    var tid = monster.id
    this.monsters[tid].freeze();
    
  }

}

